#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# ----------------------------------------------------------
# Get the file base info
# The Size, Date and MD5
# ----------------------------------------------------------
# Author: lmay.Zhou
# Blog: www.lmaye.com
# Date: 2017年7月11日 11:42:58
# ----------------------------------------------------------

import hashlib
import os
from time import strftime, localtime
from files.logs import Logger


class FileBaseInfo(object):
    """
        获取文件基础信息\n
        :param filePath 文件路径及名称\n
        :return size(文件大小), date(修改日期), md5(MD5值)
    """
    _log = Logger.log();

    # 静态的方法
    # @staticmethod
    def getFileBaseInfo(self, filePath):
        if not os.path.isfile(filePath):
            self._log.error("It's not file...");
            return;
        self.fileSize = os.path.getsize(filePath);
        # 文件最后修改时间
        self.fileDate = strftime('%Y/%m/%d %H:%M:%S', localtime(os.path.getmtime(filePath)));
        fileHash = hashlib.md5();
        f = open(filePath, "rb");
        while True:
            b = f.read(8096);
            if not b:
                break;
            fileHash.update(b);
        f.close();
        self.fileMd5 = fileHash.hexdigest();
        self._log.info(self.toString());
        return self;

    @property
    def fileDate(self):
        return self._fileDate;

    @fileDate.setter
    def fileDate(self, value):
        self._fileDate = value;

    @property
    def fileSize(self):
        return self._fileSize;

    @fileSize.setter
    def fileSize(self, value):
        self._fileSize = value;

    @property
    def fileMd5(self):
        return self._fileMd5;

    @fileMd5.setter
    def fileMd5(self, value):
        self._fileMd5 = value;

    def toString(self):
        return "FileBaseInfo [fileSize = " + self.fileSize + ", fileDate = " + self.fileDate + ", fileMd5 = " + self.fileMd5 + "]";
